package com.vtest.mes.utils;

import com.vtest.mes.domain.CQLAutoPKLEntity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.text.TabableView;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: MesHelper
 * @author: tiger
 * @create: 2024-12-10 15:35
 * @Description : excel工具类
 */

public class ExcelUtils {

    /**
     * @Description: 将一个excel转化为 list
     * @Date 2024/12/11 16:52
     * @Param excelFile : 需要转的excel的全路径
     * @Param excelTitleEntityCla : excel对应的实体类
     * @return 返回一个 excel实体类的list
     */
    public static <T> List<T> excel2List(String excelFile, Class<T> excelTitleEntityCla) {
        if (!excelFile.endsWith(".xlsx")) return null;
        List<T> dataList = new ArrayList<>();

        try (FileInputStream in = new FileInputStream(excelFile)) {
            Workbook workbook = new XSSFWorkbook(in);

            Sheet sheet = workbook.getSheetAt(0);  //

            int lastRowNum = sheet.getLastRowNum(); //获取sheet的行数(从0开始) - 可能会有那种空的行

            for (int i = 1; i < lastRowNum; i++) {
                Row row = sheet.getRow(i);

                T excelTitleEntity = excelTitleEntityCla.newInstance();       // 创建对象
                Field[] fieldNums = excelTitleEntityCla.getDeclaredFields();  //获取对象的属性列表

                int lastCellNum = row.getLastCellNum(); //获取每一行的单元格数(从1开始)
                for (int j = 0; j < lastCellNum; j++) {
                    Cell cell = row.getCell(j);
                    if (cell == null) continue;
                    Object cellValue = null;      //根据类型获取单元格中的值
                    switch (cell.getCellTypeEnum()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            cellValue = cell.getNumericCellValue();
                            break;
                        case BOOLEAN:
                            cellValue = cell.getBooleanCellValue();
                            break;
                    }
                    fieldNums[j].setAccessible(true);
                     fieldNums[j].set(excelTitleEntity, cellValue);  //给对应的属性赋值
                }
                if (!iSNull(excelTitleEntity))dataList.add(excelTitleEntity);    // 如果对象的属性有一个不为空就放入
            }


        } catch (Exception exception) {
            exception.printStackTrace();
        }


        return dataList;
    }

    /**
     * @Description: 判断一个实体类的属性是否全部为空
     * @Date 2024/12/11 16:48
     * @Param [t] - 判断的对象
     * @return true-空 | false-不空
     */
    public static <T> boolean iSNull(T t) {

        Field[] fields = t.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object o = field.get(t);
                if (o != null) return false;
            }

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return true;
    }


    public static void main(String[] args) {

        excel2List("C:\\Users\\5163\\Desktop\\12.11NJ-01.xlsx", CQLAutoPKLEntity.class);
    }
}
